﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Gift.WebApp;
using Gift.DAL;
using Gift.Model;

namespace WebApp.admin.warehouse
{
    public partial class warehouseGoodsInventory : BaseAdmin
    {

        protected string kw = "";
        protected string order = "id";
        protected int ot = 0;
        protected int pageCount = 0;
        protected int pageIndex = 1;
        protected int pageSize = 20;
        protected PageList pl = new PageList();

        tbWarehouseGoodsInventory oWarehouseGoodsInventory;
        tbWarehouseGoodsInventoryDetail oWarehouseGoodsInventoryDetail;

        protected int t = 0;
        protected int total = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ValidateLoginRule(Gift.Model.ENUM.ERule.WarehousesInventory);
                this.DeleteItem();
                this.BindWarehouse();
                this.BindDetail();
                this.BuildForm();
            }
        }


        void BindWarehouse()
        {
            DataTable dt = BaseFactory.theWarehouse.GetList("companyId=" + base.Administrator.CompanyId).Tables[0];
            if (dt != null)
            {
                this.ddlWarehouse.DataTextField = "warehouseName";
                this.ddlWarehouse.DataValueField = "id";
                this.ddlWarehouse.DataSource = dt;
                this.ddlWarehouse.DataBind();
            }
            this.ddlWarehouse.Items.Insert(0, new ExtAspNet.ListItem("请选择仓库", ""));

        }


        // Methods
        private void BindDetail()
        {
            try
            {
                if (base.Request.QueryString["p"] != null)
                {
                    this.pageIndex = int.Parse(base.Request.QueryString["p"]);
                }
                if (base.Request.QueryString["ps"] != null)
                {
                    this.pageSize = Convert.ToInt32(base.Request.QueryString["ps"]);
                }
                if (base.Request.QueryString["or"] != null)
                {
                    this.order = base.Request.QueryString["or"];
                }

                if (Request.QueryString["ot"] == "ad")
                {
                    this.pl.TblName = "view_goodsWarehouse";
                    this.pl.StrGetFields = "0 as Id, 0 as inventoryId, id as goodsId, goodsName, goodsNo, realyCount as paperCount, realyCount, 0 as differenceCount, goodsBatchNo, costPrice,warehouseId,companyId";

                }
                else
                {
                    this.pl.TblName = "tbWarehouseGoodsInventoryDetail";
                    this.pl.StrGetFields = "Id, inventoryId, goodsId, goodsName, goodsSpe, goodsNo, paperCount, realyCount, differenceCount, goodsBatchNo, costPrice";   
                }
              
                this.pl.FldName = this.order;
                this.pl.PageSize = this.pageSize;
                this.pl.PageIndex = this.pageIndex;
                this.pl.DoCount = 1;
                this.pl.OrderType = this.ot;
                if (Request.QueryString["ot"] == "ad")
                {
                    this.pl.StrWhere = " warehouseId = " + this.ddlWarehouse.SelectedItem.Value + " and  companyId=" + base.Administrator.CompanyId + "";
                }
                else
                {
                    this.pl.StrWhere = " inventoryId=" + Request.QueryString["id"];
                }
                this.total = Convert.ToInt32(BaseFactory.thePageList.GetPageList(this.pl).Rows[0]["Total"]);
                this.pl.DoCount = 0;
                DataTable pageList = BaseFactory.thePageList.GetPageList(this.pl);
                new PageListNo().DelPageSize(pageList, this.total);

                if (Request.QueryString["ot"] == "ad")
                {
                    this.Grid1.Columns[10].Hidden = true;
                }else if(Request.QueryString["ot"] == "view")
                {
                    this.Grid1.Columns[10].Hidden = true;
                    this.Grid1.EnableCheckBoxSelect = false;
                }
                else
                {
                    this.Grid1.EnableCheckBoxSelect = false;
                }
                this.Grid1.DataSource = pageList;
                this.Grid1.DataBind();
              
            }
            catch
            {
            }
        }
        protected string InventoryId
        {
            get
            {
                return Request.QueryString["id"]; 
            }
        }

        void DeleteItem()
        {
            if (Request.QueryString["action"] == "del" && Request.QueryString["inventoryid"] != null)
            {
                if (BaseFactory.theWarehouseGoodsInventoryDetail.Delete(int.Parse(Request.QueryString["inventoryid"])))
               {
                    ExtAspNet.Alert.Show("删除成功！");
               }
            }
        }

        void BuildForm()
        {
            if (Request.QueryString["id"] != null)
            {
                oWarehouseGoodsInventory = BaseFactory.theWarehouseGoodsInventory.GetModel(int.Parse(Request.QueryString["id"]));
                this.txtTheme.Value = oWarehouseGoodsInventory.theme;
                this.txtStartDate.Value = oWarehouseGoodsInventory.inventoryTime.ToString("yyyy-MM-dd HH:mm:SS");
                this.txtaRemark.InnerText = oWarehouseGoodsInventory.inventoryRemark;
                if (this.ddlWarehouse.Items.FindByValue(oWarehouseGoodsInventory.warehouseId.ToString()) != null)
                {
                    this.ddlWarehouse.Items.FindByValue(oWarehouseGoodsInventory.warehouseId.ToString()).Selected = true;
                }
            }
            if (Request.QueryString["ot"] == "view")
            {
                this.txtaRemark.Disabled = true;
                this.txtStartDate.Disabled = true;
                this.txtTheme.Disabled = true;
                this.ddlWarehouse.Enabled = false;
            }
        }


        protected void ddlWarehouse_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindDetail();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            oWarehouseGoodsInventory = new tbWarehouseGoodsInventory();
            oWarehouseGoodsInventory.inventoryAdminId = int.Parse(base.Administrator.AdminUserId);
            oWarehouseGoodsInventory.inventoryRemark = this.txtaRemark.InnerText.Trim();
            oWarehouseGoodsInventory.inventoryStates = 0;
            oWarehouseGoodsInventory.inventoryTime = DateTime.Parse(this.txtStartDate.Value);
            oWarehouseGoodsInventory.theme = this.txtTheme.Value.Trim();
            oWarehouseGoodsInventory.warehouseId = int.Parse(this.ddlWarehouse.SelectedItem.Value);
           oWarehouseGoodsInventory.id = BaseFactory.theWarehouseGoodsInventory.Add(oWarehouseGoodsInventory);

            int selectedCount = this.Grid1.SelectedRowIndexArray.Length;
            for (int i = 0; i < selectedCount; i++)
            {
                int rowIndex = this.Grid1.SelectedRowIndexArray[i];
              
                oWarehouseGoodsInventoryDetail = new tbWarehouseGoodsInventoryDetail();
                oWarehouseGoodsInventoryDetail.costPrice =  decimal.Parse( this.Grid1.Rows[rowIndex].Values[5].ToString());
                oWarehouseGoodsInventoryDetail.differenceCount = 0;
                oWarehouseGoodsInventoryDetail.goodsBatchNo = this.Grid1.Rows[rowIndex].Values[9].ToString();
                oWarehouseGoodsInventoryDetail.goodsId = int.Parse( this.Grid1.Rows[rowIndex].Values[2].ToString());
                oWarehouseGoodsInventoryDetail.goodsName = this.Grid1.Rows[rowIndex].Values[4].ToString();
                oWarehouseGoodsInventoryDetail.goodsNo = this.Grid1.Rows[rowIndex].Values[3].ToString();
                oWarehouseGoodsInventoryDetail.goodsSpe = "";
                oWarehouseGoodsInventoryDetail.inventoryId = oWarehouseGoodsInventory.id;
                oWarehouseGoodsInventoryDetail.paperCount = int.Parse(this.Grid1.Rows[rowIndex].Values[6].ToString());
                oWarehouseGoodsInventoryDetail.realyCount = int.Parse(this.Grid1.Rows[rowIndex].Values[7].ToString());
                BaseFactory.theWarehouseGoodsInventoryDetail.Add(oWarehouseGoodsInventoryDetail);
            }
            ExtAspNet.Alert.Show("添加成功", "", "window.location.href='warehouseGoodsInventoryList.aspx';");
       

        }

        protected void Grid1_RowDataBound(object sender, ExtAspNet.GridRowEventArgs e)
        {

            //if (e.DataItem == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            //{
            //    if (!isEdit)
            //    {
            //        e.Item.FindControl("divEdit").Visible = false;
            //    }
            //    if (!isDeleted)
            //    {
            //        e.Item.FindControl("divDeleted").Visible = false;
            //    }
            //}
        }

 


    }
}